/**
 * 版权所有：bmy
 * 邮箱：2271608011@qq.com
 * 备注：代码没啥好看的，写给自己用
 */

setTimeout(() => {

  let currentURL = location.href
  let webSiteUrl = {
    csdn: "blog.csdn.net",
    zhihu: {
      home: "www.zhihu.com",
      zhuanlan: "zhuanlan.zhihu.com"
    },
    jianshu: "www.jianshu.com",
    juejin: "juejin.cn"
  }

  let clearHref = (el, tarAttr = "?target=") => {
    $(el).eq(0).find("a").each(function () {
      let href = $(this).attr("href")
      if (href != undefined && href.includes(tarAttr)) {
        $(this).attr("href", decodeURIComponent(href.split(tarAttr)[1]))
      }
    })
  }

  let clearCopy = (el) => {
    let newArticle = $(el).eq(0).clone()
    $(el).eq(0).replaceWith(newArticle)
  }


  if (currentURL.includes(webSiteUrl.csdn)) {
    // 分析源网站的代码，链接是这个：
    // https://csdnimg.cn/release/blogv2/dist/pc/js/detail-0ce0e42bff.min.js

    // https://csdnimg.cn/release/blogv2/dist/pc/js/detail-4c4f40c1d8.min.js
    // https://g.csdnimg.cn/common/csdn-toolbar/csdn-toolbar.js

    // 自动展开所有折叠的代码
    $(".hide-preCode-bt").click()

    window.articleType = -1
    window.copyPopSwitch = false

    clearCopy("#content_views")

    // 重新修复文章正文a标签的所有点击事件
    $("#content_views").on('click', "a", function (t) {
      var a = $(this).attr("href") || "";
      t.preventDefault()
      window.open(a, "_blank")
    });


  } else if (
    currentURL.includes(webSiteUrl.zhihu.zhuanlan) ||
    currentURL.includes(webSiteUrl.zhihu.home)
  ) {

    // 展开问题的全文
    $(".QuestionRichText-more").click()

    clearHref("body")

    let eachContent = () => $(".RichContent").each(function () { clearCopy(this) })
    eachContent()
    
    let Observe = new MutationObserver( (mutations, observer) => eachContent());
    Observe.observe($(".css-0")[0], { 'childList': true, 'attributes': true });

  } else if (currentURL.includes(webSiteUrl.jianshu)) {

    clearCopy("div[role=main] section")
    clearHref("div[role=main] section", "?t=")


  } else if (currentURL.includes(webSiteUrl.juejin)) {
    clearCopy("#article-root")
    clearHref(".main-area .article")
  }


}, 300)